package com.yunsom.testdemo.multiple_thread.pipe_stream;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 管道流是用来在多个线程之间进行信息传递的Java流。
 */
public class PipeStream {
    public static void main(String[] args) {
        // 创建一个线程池
        ExecutorService executorService = Executors.newCachedThreadPool();

        try {
            // 创建输入输出管道流
            PipedOutputStream pos = new PipedOutputStream();
            PipedInputStream pis = new PipedInputStream(pos);

            // 创建发送线程和接受线程
            Sender sender = new Sender(pos);
            Receiver receiver = new Receiver(pis);

            // 提交给线程池运行发送线程和接受线程
            executorService.execute(sender);
            executorService.execute(receiver);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
